package com.sjg.rts.util;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;

/**
 * Created by fuqingjian on 2017/2/3.
 */
public class HttpUtilsT {
    public static String HttpGetRequest(String url){
        String result = "";
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
//            System.setProperty("http.maxRedirects", "100");
//            CookieManager manager=new CookieManager(null, CookiePolicy.ACCEPT_ALL);
//            CookieHandler.setDefault(manager);
            HttpURLConnection connection =  (HttpURLConnection) realUrl.openConnection();
            connection.setRequestProperty("Host","www.aa234.com");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0");
            connection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
            connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            connection.setRequestProperty("Accept-Encoding","gzip, deflate");
            connection.setRequestProperty("Referer","http://www.aa234.com/cqssc/");
            connection.setRequestProperty("X-Requested-With","XMLHttpRequest");
            connection.setRequestProperty("Cookie","ca=3; PHPSESSID=obk2mcg0bp0sik4bg1544klsm3");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Connection", "keep-alive");
            connection.setRequestProperty("Pragma", "no-cache");
            connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setRequestProperty("Accept-Charset","utf-8");
            connection.setRequestMethod("GET");
            connection.setUseCaches(false);
            connection.setConnectTimeout(2500);
            connection.setReadTimeout(4500);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println( e);
        }
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
    public static String HttpGetRequest1(String url){
        String result="";
        try {
            // 根据地址获取请求
            HttpGet request = new HttpGet(url);//这里发送get请求
            request.setHeader("Host","www.aa234.com");
            request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0");
            request.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            request.setHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            request.setHeader("Accept-Encoding","gzip, deflate");
            request.setHeader("Referer","http://www.aa234.com/cqssc/");
            request.setHeader("X-Requested-With","XMLHttpRequest");
            request.setHeader("Cookie","ca=3; PHPSESSID=obk2mcg0bp0sik4bg1544klsm3");
            request.setHeader("Content-Type", "application/x-www-form-urlencoded");
            request.setHeader("Connection", "keep-alive");
            request.setHeader("Pragma", "no-cache");
            request.setHeader("Cache-Control", "no-cache");
            // 获取当前客户端对象
            HttpClient httpClient = new DefaultHttpClient();
            // 通过请求对象获取响应对象
            HttpResponse response = httpClient.execute(request);

            // 判断网络连接状态码是否正常(0--200都数正常)
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return EntityUtils.toString(response.getEntity(),"utf-8");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    public static void main(String[] args) {
        String str=HttpGetRequest("http://www.aa234.com/cqssc/getData?t=0.5156916745317565");
        System.out.println(str);
    }
}
